package common.tika;

import org.apache.tika.Tika;
import org.apache.tika.language.LanguageIdentifier;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.parser.txt.TXTParser;
import org.apache.tika.sax.BodyContentHandler;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;

/**
 *Apache Tika库
 *  内容检测和分析框架 Tika
 *  Apache Tika是一种用于从各种文件格式进行文档类型检测和内容提取的库。
 *  在内部，Tika使用各种现有的文档解析器和文档类型检测技术来检测和提取数据。
 *  使用Tika，可以开发通用型检测器和内容提取器，以在一定程度上提取结构化文本以及来自不同类型文档（如电子表格，文本文档，图像，PDF，甚至多媒体输入格式）的元数据。
 *  Tika提供了一个单一的通用API来解析不同的文件格式。它为每个文档类型使用83个现有的专门的解析器ibraries。
 *
 *  学习地址： http://www.vue5.com/tika/tika_overview.html
 */
public class Tika_Demo {

    public static void main(String[] args) {
        getContext();
        parseMeta();
        LanguageDetection();
        parseExcel();
    }

    @Test
    public void getFileType() {
        try {
            File file = new File("D:\\文件\\个人\\tada\\文件一.zip");
            Tika tika = new Tika();
            String fileType = tika.detect(file);
            if (fileType != null && fileType.contains("/")) {
                fileType = fileType.substring(fileType.indexOf("/") + 1);
            }
            System.out.println(fileType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getContext() {

        try {

            File file = new File("D:\\文件\\个人\\tada\\文件一.txt");
            Tika tika = new Tika();

            String filecontent = tika.parseToString(file);

            System.out.println("Extracted Content: " + filecontent);

            return filecontent;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }

    /**
     * Tika元数据提取
     *
     * @return
     */
    public static String parseMeta() {

        try {

            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream inputstream = new FileInputStream(new File("D:\\文件\\个人\\tada\\易木采购管理实训平台V2.0-产品介绍.pdf"));
            ParseContext pcontext = new ParseContext();
            PDFParser pdfparser = new PDFParser();
            pdfparser.parse(inputstream, handler, metadata, pcontext);

            System.out.println("Contents of the PDF :" + handler.toString());
            // 元数据提取
            System.out.println("Metadata of the PDF:");

            String[] metadataNames = metadata.names();

            for (String name : metadataNames) {

                System.out.println(name + " : " + metadata.get(name));

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }

    /**
     * 语言检测
     */
    public static String LanguageDetection() {

        try {
            Parser parser = new AutoDetectParser();
            BodyContentHandler handler = new BodyContentHandler();
            Metadata metadata = new Metadata();
            FileInputStream content = new FileInputStream(new File("D:\\文件\\个人\\tada\\文件一.txt"));
            //Parsing the given document
            parser.parse(content, handler, metadata, new ParseContext());
            LanguageIdentifier object = new LanguageIdentifier(handler.toString());
            System.out.println("Language name :" + object.getLanguage());
            return object.getLanguage();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }

    /**
     * PDF提取
     */
    public static String parsePdf() {

        try {

            BodyContentHandler handler = new BodyContentHandler();

            Metadata metadata = new Metadata();

            FileInputStream inputstream = new FileInputStream(new File("D:\\1111.pdf"));

            ParseContext pcontext = new ParseContext();


            //parsing the document using PDF parser

            PDFParser pdfparser = new PDFParser();

            pdfparser.parse(inputstream, handler, metadata, pcontext);

            //getting the content of the document

            System.out.println("Contents of the PDF :" + handler.toString());

            // 元数据提取

            System.out.println("Metadata of the PDF:");

            String[] metadataNames = metadata.names();

            for (String name : metadataNames) {

                System.out.println(name + " : " + metadata.get(name));

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }

    /**
     * Txt提取
     */
    public static String parseTxt() {

        try {

            BodyContentHandler handler = new BodyContentHandler();

            Metadata metadata = new Metadata();

            FileInputStream inputstream = new FileInputStream(new File("D:\\example.txt"));

            ParseContext pcontext = new ParseContext();

            //Text document parser

            TXTParser TexTParser = new TXTParser();

            TexTParser.parse(inputstream, handler, metadata, pcontext);

            System.out.println("Contents of the document:" + handler.toString());

            return handler.toString();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }

    /**
     * Excel 提取
     */
    public static String parseExcel() {

        try {

            BodyContentHandler handler = new BodyContentHandler();

            Metadata metadata = new Metadata();

            FileInputStream inputstream = new FileInputStream(new File("D:\\文件\\个人\\tada\\地推活跃用户导出_2022-03-30 19_23_06.xlsx"));

            ParseContext pcontext = new ParseContext();

            OOXMLParser msofficeparser = new OOXMLParser();

            msofficeparser.parse(inputstream, handler, metadata, pcontext);

            System.out.println("Contents of the document:" + handler.toString());

            return handler.toString();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return "";

    }
}
